package com.yanqu.road.server.cmd.strongholdwar.wild;

import com.yanqu.road.entity.activity.stronghold.StrongholdOpenBoxResult;
import com.yanqu.road.entity.activity.stronghold.StrongholdWildBox;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.pb.CrossStrongholdPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_WILD_OPEN_BOX,desc = "开启宝箱")
public class CrossStrongholdOpenBoxCmd extends CrossUnionCommand {
    private int activityId;
    private int boxId;
    private long userId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossStrongholdOpenBoxReqMsg req = StrongholdWarProto.CrossStrongholdOpenBoxReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        boxId = req.getBoxId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        StrongholdOpenBoxResult openBoxResult = CrossStrongholdMgr.openBox(activityId, serverId, userId, boxId);
        StrongholdWarProto.CrossStrongholdOpenBoxRespMsg.Builder resp = StrongholdWarProto.CrossStrongholdOpenBoxRespMsg.newBuilder();
        ret = openBoxResult.getRet();
        resp.setRet(ret);
        if (ret == 0) {
            resp.setReward(openBoxResult.getReward());
            for (StrongholdWildBox box : openBoxResult.getBoxList()) {
                StrongholdWarProto.StrongholdBoxDataTempMsg.Builder boxMsg = CrossStrongholdPb.getBoxMsg(box);
                resp.addBox(boxMsg);
            }
            resp.setReward(openBoxResult.getReward());
        }
        YanQuMessage msg = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_STRONGHOLD_WILD_OPEN_BOX, resp);
        MessageHelper.sendPacket(serverId, userId, msg);
    }

    @Override
    public void sendData() {

    }
}
